package com.yuxi.interceptor;


import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
import org.springframework.mobile.device.DeviceUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author sunke
 * @Date 2020/11/27 12:21
 * @description 手机过滤
 */
@Component(value = "phoneDeviceResolverHandlerInterceptor")
public class PhoneDeviceResolverHandlerInterceptor extends DeviceResolverHandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.preHandle(request, response, handler);

        Device device = DeviceUtils.getCurrentDevice(request);

        if (device.isMobile() && request.getRequestURI().indexOf(("/wap/"))<0) {
            response.sendRedirect("/wap"+request.getRequestURI());
            return true;
        }

        return true;
    }
}
